package com.yufubao.service.service.common.impl;

import com.google.common.collect.ImmutableMap;
import com.yufubao.framework.enums.CommonExceptionEnum;
import com.yufubao.framework.exception.BaseException;
import com.yufubao.framework.result.ResponseResult;
import com.yufubao.service.service.common.OssService;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.yufubao.framework.constant.OssConstant.*;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/9/4 23:23
 */
@Slf4j
@Service("ossService")
public class OssServiceImpl implements OssService {

    @Autowired
    private MinioClient minioClient;

    @Value("${minio.endpoint}")
    String endpoint;

    /**
     * @return com.yufubao.framework.result.ResponseResult
     * @description 获取上传营业执照签名信息
     * @author zhangyiheng
     * @date 2024/9/4 23:23
     */
    @Override
    public ResponseResult getLicencePolicy(String objectName) {
        Map<String, String> map = getPolicy(objectName, TYPE_APPLET, true);
        return ResponseResult.okResult(map);
    }

    //http://192.168.56.30:9000/business-license/1111/111.jpg
    private Map<String, String> getPolicy(String objectName, String type, Boolean license) {
        String product = null;
        Map<String, String> map = null;
        try {
            Map<String, String> reqParams = new HashMap<String, String>();
            reqParams.put("response-content-type", "application/json");

            //配置文件名
            Long id = 1L;
//            switch (type) {
//                case TYPE_APPLET:
//                    id = checkUserLoc(AppletInterceptor.APPLETTHREADLOCAL).getUserId();
//                    break;
//                case TYPE_MANAGER:
//                    id = checkManagerLoc(AppletInterceptor.MANAGERTHREADLOCAL).getManagerId();
//                    break;
//                default:
//                    log.error("获取文件服务签名错误");
//                    throw new BaseException(CommonExceptionEnum.NOT_PERMISSION);
//            }

            String sdf = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            objectName = sdf + "/" + id + "_" + objectName;

            String bucket = license ? BUSINESS_LICENSE : APPLET_IMAGES;

            String path = endpoint + "/" + bucket + "/" + objectName;

            product = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .method(Method.PUT) //这里必须是PUT，如果是GET的话就是文件访问地址了。如果是POST上传会报错.
                            .bucket(bucket)
                            .object(objectName)
                            .expiry(EXPIRY, TimeUnit.MINUTES) //url有效期10分钟
                            .extraQueryParams(reqParams)
                            .build());

            map = ImmutableMap.of("product", product, "path", path);

        } catch (Exception e) {
            log.error("获取上传营业执照签名信息错误：{}", e.getMessage());
            throw new BaseException(CommonExceptionEnum.MINIO_ERROR);
        }
        return map;
    }

    /**
     * @param objectName
     * @param type
     * @return com.yufubao.framework.result.ResponseResult
     * @description 获取上传一般图片签名信息
     * @author zhangyiheng
     * @date 2024/9/5 09:04
     */
    @Override
    public ResponseResult getOrdinaryPolicy(String objectName, String type) {
        Map<String, String> map = getPolicy(objectName, type, false);
        return ResponseResult.okResult(map);
    }
}
